package com.fashion.schedule;

import com.fashion.model.News;
import com.fashion.service.NewsService;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.List;

/**
 * User: gnyk
 * Date: 13-2-24
 * Save all news to html files
 */
public class ScheduledNewsPageSaver {
    private static final Logger logger = Logger.getLogger(ScheduledNewsPageSaver.class);
    private NewsService newsService;
    private String urlPrefix;
    private String basePersistenceFolder;

    public void saveAllNewsToHtml() {
        int count = 0;
        List<News> newsList = newsService.findAllNews();
        logger.info("start to save news html");
        for (News news : newsList) {
            String id = news.getId();
            try {
                if (id.startsWith("13")) {
                    newsService.saveHtmlNews(urlPrefix + "n-" + id + ".html", urlPrefix, basePersistenceFolder);
                    count++;
                }

            } catch (IOException ex) {
                logger.error("503 error for id: " + id + " message: " + ex.getMessage());
            } catch (Exception e) {
                logger.error("could not save news: " + id, e);
            }
        }

        logger.info("test saved news html");
        logger.info("saved news html with count: " + count);
    }

    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    public void setUrlPrefix(String urlPrefix) {
        this.urlPrefix = urlPrefix;
    }

    public void setBasePersistenceFolder(String basePersistenceFolder) {
        this.basePersistenceFolder = basePersistenceFolder;
    }
}
